﻿using CarEdge.Repository.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CarEdge.Repository.Instance
{
    /// <summary>
    /// MySql数据实体方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MySql_Repository<T> : I_Repository<T> where T : class, new()
    {
        private readonly DbContext _dbContext;

        public MySql_Repository(DbContext dbContext)
        {
            _dbContext = dbContext;

        }
        /// <summary>
        /// 新增单个实体
        /// </summary>
        /// <param name="entity"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Add(T entity)
        {
            _dbContext.Add<T>(entity);
        }
        /// <summary>
        /// 新增多个
        /// </summary>
        /// <param name="id"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void AddRange(IEnumerable<T> ie)
        {
           _dbContext.AddRange(ie);
        }
        /// <summary>
        /// 删除单个
        /// </summary>
        /// <param name="entity"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Delete(T entity)
        {
           _dbContext.Remove<T>(entity);    
        }
        /// <summary>
        /// 删除多个
        /// </summary>
        /// <param name="ie"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void DeleteRange(IEnumerable<T> ie)
        {
            _dbContext.RemoveRange(ie);
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>>? where = null)
        {
            var iq = (IQueryable<T>)_dbContext.Set<T>();
            if (where != null)
            {
                iq = iq.Where(where);
            }
            return iq;
        }

        public IQueryable<T> QueryAll<type>(bool isAsc, Expression<Func<T, type>> order, Expression<Func<T, bool>>? where = null)
        {
            var iq = this.QueryAll(where);
           return isAsc ? iq.OrderBy(order):iq.OrderByDescending(order);
        }

        public IQueryable<T> QueryAll<type>(out int total, int skip, int take, bool isAsc, Expression<Func<T, type>> order, Expression<Func<T, bool>>? where = null)
        {
            var iq = this.QueryAll(isAsc, order, where);
            total = iq.Count();
            return iq.Skip(skip).Take(take);
        }




        /// <summary>
        /// 保存 
        /// </summary>
        /// <returns></returns>

        public int SaveChanges()
        {
            return _dbContext.SaveChanges();
        }

        /// <summary>
        /// 异步保存
        /// </summary>
        /// <returns></returns>
        public Task<int> SaveChangesAsync()
        {
            return _dbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 修改单个
        /// </summary>
        /// <param name="entity"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Update(T entity)
        {
           _dbContext.Entry<T>(entity).State=EntityState.Modified;
        }
        /// <summary>
        /// 修改多个
        /// </summary>
        /// <param name="ie"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateRange(IEnumerable<T> ie)
        {
            foreach(var item in ie)
            {
                this.Update(item);
            }
        }
    }
}
